﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using LifeShare1.Media;
using LifeShare1.Models;
namespace LifeShare1.Controllers
{
    public class BookController : Controller
    {
        //
        // GET: /Book/
        private MediaService myService = new MediaService();
        public ActionResult BookIndex(int page=0)
        {
            int pagesize = 10;
            if (page * pagesize > myService.GetAllBookInfoes().Count())
                page -= 1;
            if (page <= 0)
                page = 1;
            ViewData["page"] = page;
            BookInfo[] model = myService.GetAllBookInfoes();
            return View(model);
        }
        public ActionResult BookInfoDetails(int resourceID)
        {
            BookInfo book = myService.GetBookInfo(resourceID);
            ViewData["MyTitle"] = book.title;
            ViewData["MyAuthor"] = book.author;
            ViewData["MyImageURL"] = book.imageURL;
            ViewData["MyPublisher"] = book.publisher;
            ViewData["MyPubDate"] = book.pubdate;
            ViewData["MyPages"] = book.pages;
            ViewData["MyPrice"] = book.price;
            ViewData["MyISBN"] = book.isbn13;
            ViewData["MyRatingAverage"] = book.ratingAverage;
            ViewData["MyNumRaters"] = book.numRaters;
            ViewData["MyAuthorIntro"] = book.authorIntro;
            ViewData["MySummary"] = book.summary;
            Comment[] comments = myService.GetCommentList(resourceID);
            if (comments.Count() == 0)
            {
                myService.AddComment(-1, resourceID, "");
            }
            comments = myService.GetCommentList(resourceID);
            return View(comments);
        }
        
        public ActionResult AddBookComment(int resourceID)
        {
            BookInfo book = myService.GetBookInfo(resourceID);
            ViewData["MyTitle"] = book.title;
            ViewData["MyAuthor"] = book.author;
            ViewData["MyImageURL"] = book.imageURL;
            ViewData["MyPublisher"] = book.publisher;
            ViewData["MyPubDate"] = book.pubdate;
            ViewData["MyPages"] = book.pages;
            ViewData["MyPrice"] = book.price;
            ViewData["MyISBN"] = book.isbn13;
            ViewData["MyRatingAverage"] = book.ratingAverage;
            ViewData["MyNumRaters"] = book.numRaters;
            ViewData["MyAuthorIntro"] = book.authorIntro;
            ViewData["MySummary"] = book.summary;
            ViewData["MyResourceID"] = book.ResourceID;
            Comment[] comments = myService.GetCommentList(resourceID);
            if (comments.Count() == 0)
            {
                myService.AddComment(-1, resourceID, "");
            }
            comments = myService.GetCommentList(resourceID);
            return View(comments);
        }
        
        [AcceptVerbs(HttpVerbs.Post), ActionName("AddBookComment")]
        public ActionResult AddCommentClick(int userID, int resourceID,string NewComment)
        {
            if (NewComment == "")
            {
                ModelState.AddModelError("NewComment", "评论不能为空");
            }
            else
            {
                myService.AddComment(userID, resourceID, NewComment);
            }
            return BookInfoDetails(resourceID);
        }
        [AcceptVerbs(HttpVerbs.Post), ActionName("BookIndex")]
        public ActionResult AddBook(string ISBNInput)
        {
            string result = myService.AddBook(ISBNInput);
            if (result == "Invalid isbn")
            {
                ModelState.AddModelError("ISBN", "ISBN无效");
                return BookIndex();
            }
            else
            {
                return BookIndex();
            }

        }
        
    }
    
}
